package com.ilink.groundservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.PO.langflowApp.CampApp;
import com.ilink.groundservice.entity.VO.langflowApp.CampHomeworkStatus;
import com.ilink.groundservice.service.CampAppService;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.log.method.MethodLog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ground/camp_app")
public class CampAppController {

    private final CampAppService campAppService;

    @MethodLog("分页查询学员作业提交情况")
    @GetMapping("/get")
    public R getCampAppPage(
        @RequestParam String camp,
        @RequestParam(required = false) String keywords,
        @RequestParam(required = false) String tag,
        @RequestParam int page,
        @RequestParam int size) {
        try {
            IPage<CampApp> result = campAppService.getCampAppByCondition(camp, keywords, tag, page, size);
            return R.ok("查询成功",result);
        } catch (Exception e) {
            log.error("查询学员作业提交情况失败，训练营 {}, 页码 {}, 关键词 {}, 厂商 {}，分页大小 {}，错误信息: {}",
                camp, page, keywords, tag, size, e.getMessage());
            return R.error("查询学员作业提交情况失败");
        }
    }

    @MethodLog("插入学员作业提交记录")
    @PostMapping("/insert")
    public R insertCampApp(@RequestBody CampApp campApp) {
        try {
            boolean success = campAppService.insertCampApp(campApp);
            return success ? R.ok("插入成功",true) : R.error("插入失败");
        } catch (Exception e) {
            log.error("插入学员作业提交记录失败，记录内容：{}，错误信息：{}", campApp, e.getMessage());
            return R.error("插入学员作业提交记录失败");
        }
    }

    @MethodLog("查询学员提交的作业")
    @GetMapping("/homework")
    public R getSubmittedAssignments(
        @RequestParam String studentId,
        @RequestParam String camp) {
        try {
            List<CampHomeworkStatus> assignments = campAppService.getSubmittedAssignments(studentId, camp);
            return R.ok("查询成功",assignments);
        } catch (Exception e) {
            log.error("查询学员作业提交记录失败，学员ID：{}，训练营编号：{}，错误信息：{}", studentId, camp, e.getMessage());
            return R.error("查询学员作业提交记录失败");
        }
    }
}
